package com.sifeng.utils;

import java.util.ArrayList;

/**
 * Created by yang_gsh on 2018/12/10.
 */
public class SqlUtils {
	public static String formatSentence(String sqlSentence, ArrayList list) {
		StringBuffer result = new StringBuffer();
/*		int off = false;
		int idx = false;*/
		int k = 0;
		ArrayList list1 = (ArrayList)list.clone();
		ArrayList delIdxList = new ArrayList();

		int i;
		for(i = 0; i < sqlSentence.length(); ++i) {
			if(sqlSentence.charAt(i) == 63) {
				result.append("'" + list1.get(k) + "'");
				++k;
			}/* else if(sqlSentence.charAt(i) == 37 && i + 1 < sqlSentence.length() && sqlSentence.charAt(i + 1) == 115) {
				if(list1 != null && k < list1.size()) {
					if(escape) {
						result.append(sqlEscape((String)list1.get(k)));
					} else {
						result.append(list1.get(k));
					}

					delIdxList.add(Integer.valueOf(k));
				} else {
					result.append("%s");
				}

				++k;
				++i;
			} */else {
				result.append(sqlSentence.charAt(i));
			}
		}

		if(sqlSentence.charAt(i - 1) != 91) {
			result.append(";");
		}

		list.clear();

		for(i = 0; i < list1.size(); ++i) {
			if(!delIdxList.contains(Integer.valueOf(i))) {
				list.add(list1.get(i));
			}
		}

		return result.toString();
	}

	/*public static String sqlEscape(String value) {
		return StringUtils.replace(value, "'", "''");
	}*/
}
